﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;


    class sqLiteHelper
    {
        public static readonly string ConnectionString = @"Data Source=|DataDirectory|Testdb.db3";
        /// <summary>
        /// 执行SQL操作
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句</param>
        /// <param name="commandParameters">参数</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
        {
            return ExecuteNonQuery(ConnectionString, cmdType, cmdText, commandParameters);
        }
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
        {
            //try
            //{
            SQLiteCommand cmd = new SQLiteCommand();
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
                int num = 0;
                num = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                connection.Dispose();
                return num;
            }
            //}
            //catch (Exception ee)
            //{
            //    Function.WriteFile("dbLog.txt", ee.Message + "\r\n\r\n" + ee.Source + "\r\n\r\n", true);
            //    return 0;
            //}
        }
        public static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            //cmd.CommandTimeout = 60;
            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SQLiteParameter parameter in cmdParms)
                {
                    cmd.Parameters.Add(parameter);
                }
            }
        }
        public static void PrepareCommand(DbCommand cmd, DbConnection conn, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            //cmd.CommandTimeout = 60;
            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (DbParameter parameter in cmdParms)
                {
                    cmd.Parameters.Add(parameter);
                }
            }
        }

        public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
        {
            //Function.WriteFile("xxx.txt",ConnectionString,true );
            return ExecuteTable(ConnectionString, cmdType, cmdText, commandParameters);
        }
        public static DataTable ExecuteTable(string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters)
        {
            try
            {
                SQLiteCommand cmd = new SQLiteCommand();
                using (SQLiteConnection connection = new SQLiteConnection(connectionString))
                {

                    PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter();
                    adapter.SelectCommand = cmd;
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Result");
                    cmd.Parameters.Clear();
                    connection.Dispose();
                    if (dataSet.Tables.Count > 0)
                    {
                        return dataSet.Tables["Result"];
                    }
                    return null;

                }
            }
            catch (Exception ee)
            {
                //Function.WriteFile("dbLog.txt", ee.Message + "\r\n\r\n" + ee.Source + "\r\n\r\n", true);
                return null;
            }
        }
    }

